<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Session;

class Index extends Base
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $scoreId = Db::name('people')->where('id',session('user_id'))->value('section_id');
        $res = Db::name('section')
            ->join('y_hospital','y_hospital.id = y_section.hospital_id')
            ->where('y_section.id',$scoreId)
            ->field('y_hospital.name as hospitalName,y_section.name as sectionNmae,y_section.id,y_hospital.id as hid')
            ->find();
        $hospitalId = Db::name('section')->where('id',$scoreId)->value('hospital_id');
        $hospital = Db::name('hospital')->select();
        $section = Db::name('section')->where('hospital_id',$hospitalId)->select();
        $this->assign(['res'=>$res,'section'=>json_encode($section),'id'=>$res['id'],'hospital'=>json_encode($hospital)]);
        return $this->view->fetch();
    }
    public function section(){
        $id = $this->request->post('id');
        $name = Db::name('section')->where('id',$id)->value('name');
        return $name?$name:400;
    }
    public function hosp(){
        $id = $this->request->post('id');
        $hospName = Db::name('hospital')->where('id',$id)->value('name');
        $section = Db::name('section')->where('hospital_id',$id)->select();
        return ['hospName'=>$hospName,'section'=>json_encode($section)];
    }
    public function sectionAdd(){
        $params = $this->request->post();
        $params['people_id'] = session('user_id');
        $params['createtime'] = time();
        Db::name('people')->where('id',session('user_id'))->update(['section_id'=>$params['section_id']]);
        $res = Db::name('score')->insert($params);
        return $res?200:400;
    }
    public function tuisong()
    {
        $openid = Db::name('people')->where('id',session('user_id'))->value('openid');
        $token = info();
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token;
        $count = "我们已经收到您的评价，非常感谢您的参与！我们会仔细阅读每一份评价，认真对待每一个评分，努力改善提升服务质量。若对我们的工作有进一步的意见，请<a href='http://gzh.huixiangxuanyu.com/index/surey/index'>点击此处</a>帮我们提升服务质量吧~";
        $text_str = '{
              "touser":"'.$openid.'",
              "msgtype":"text",
              "text":
              {
                   "content":"'.$count.'"
              }
          }';
       $this->https_request("post",$url,$text_str);

    }
    function https_request($type="post",$url,$data=null)
    {
        //1、初始化curl
        $ch = curl_init();

        //2、设置传输选项
        curl_setopt($ch, CURLOPT_URL, $url);//请求的url地址
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//将请求的结果以文件流的形式返回

        // 判断请求发送的类型
        if($type == "post"){

            if(!empty($data)){
                curl_setopt($ch,CURLOPT_POST,1);//请求POST方式
                curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//POST提交的内容
            }

        }

        // 跳过SSL验证，本代码用来解决不支持SSL验证的问题（暂时使用，不推荐）
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        //3、执行请求并处理结果
        $outopt = curl_exec($ch);

        //把json数据转化成数组
        $outoptArr = json_decode($outopt,TRUE);

        //4、关闭curl
        curl_close($ch);

        //如果返回的结果$outopt是json数据，则需要判断一下
        if(is_array($outoptArr)){
            return $outoptArr;
        }else{
            return $outopt;
        }
    }

}
